package kuaishou

import (
	"gitee.com/edodo/flysdk/api"
	"github.com/jinzhu/copier"
)

func init() {
	api.RegisterBaseMpApi(api.Kuaishou, &ksMpApi{})
}

type ksMpApi struct {
}

// https://mp.kuaishou.com/docs/develop/server/getAccessToken.html
func (wc *ksMpApi) GetAccessToken(config *api.MpConfig) (at *api.AccessToken, e error) {
	// get access token by http request
	p := map[string]string{
		"app_id":     config.AppId,
		"app_secret": config.AppSecret,
		"grant_type": "client_credential",
	}
	token := new(KsAccessToken)
	if e = api.PostForm(ACCESS_TOKEN_URL, p, token); e != nil {
		//glog.Errorf("Get access token failed: %v", e)
		return
	}

	at, e = token.ToAccessToken()
	return
}

func (wc *ksMpApi) GetStableAccessToken(config *api.MpConfig, force bool) (at *api.AccessToken, e error) {
	return wc.GetAccessToken(config)
}

// https://mp.kuaishou.com/docs/develop/server/code2Session.html
func (wc *ksMpApi) Code2Session(config *api.MpConfig, at *api.AccessToken, js_code, anonymous_code string) (acs *api.Code2Session, e error) {
	p := map[string]string{
		"app_id":     config.AppId,
		"app_secret": config.AppSecret,
		"js_code":    js_code,
	}
	sessResp := new(KsCode2Session)
	if e = api.PostForm(JSCODE_SESSION_URL, p, sessResp); e != nil {
		//glog.Errorf("Get session failed: %v", e)
		return
	}
	acs = &api.Code2Session{}
	e = copier.Copy(acs, sessResp)
	if e != nil {
		return
	}
	return
}

func (wc *ksMpApi) GetPhoneNumber(at *api.AccessToken, code string) (aph *api.PhoneNumber, e error) {
	e = api.ErrUnsupportedApi
	return
}
